package com.atguigu.juc.study.chapter10.reference;

import java.lang.ref.SoftReference;
import java.util.concurrent.TimeUnit;

/**
 * @author : leimingchao
 * @date Date : 2022年06月19日 18:10
 */
public class SoftReferenceDemo {

    public static void main(String[] args) {

        SoftReference<MyObject> softReference = new SoftReference<>(new MyObject());

        // System.out.println("----softReference：" + softReference.get());

        System.gc();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("gc after内存够用：" + softReference.get());

        try {
            //20MB
            byte[] bytes = new byte[20 * 1024 * 1024];
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("gc after内存不够用：" + softReference.get());
        }
    }
}
